package com.Pounds.dao.DaoEvent;

import com.Pounds.dao.DaoController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DaoEventListener implements ApplicationListener<DaoEvent> {
    @Autowired
    private DaoController daoController;

    private void testAndInitMySQL()
    {
        if (daoController.findDatabase("trane") == null)
        {
            daoController.newDatabase("trane");
        }

        if (daoController.findTable("UserData_1") == null)
        {
            daoController.newTable("UserData_1", "( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) NOT NULL, account VARCHAR(100) NOT NULL, password VARCHAR(100) NOT NULL, is_active BOOLEAN DEFAULT TRUE )");
        }
    }


    @Override
//    @Async
    public void onApplicationEvent(DaoEvent event) {
        switch (event.getEvent())
        {
            case Normal:
                break;
            case TestAndInitMySQL:
                this.testAndInitMySQL();
                break;
        }
    }
}
//{
//        "id" : "1",         // 辅助排序，根据当前页面计算，不在数据库内的数据
//        "postid" : "xxx",   // 帖子的id，可用于请求帖子的具体地址
//        "title" : "xxx",    // 帖子标题
//        "username" : "xxx", // 帖子的发帖人
//        "timestamp" : "xxx" // 发布时间
//评论
//点赞
//}